﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Threading.Tasks;
using Plat.WebUtility.Security;

namespace Plat.WebUtility
{
    [Authorize]
    public abstract class MvcControllerBase : Controller
    {
        /// <summary>
        /// 对应api的Url
        /// </summary>
        public string ApiUrl
        {
            get;
            protected set;
        }

        /// <summary>
        /// 用户权限列表
        /// </summary>
        public UserAuthModel[] UserAuthList
        {
            get
            {
                return AuthorizedUser.Current.UserAuthList;
            }
        }

        /// <summary>
        /// 登录用户票据
        /// </summary>
        public string UserLoginTicket
        {
            get
            {
                return AuthorizedUser.Current.UserLoginTicket;
            }
        }

        /// <summary>
        /// Action执行时写入Cookie信息
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);

            //验证是否是登录用户
            var identity = filterContext.HttpContext.User.Identity;
            if (identity.IsAuthenticated)
            {
                //有效登录用户，写入用户票据信息到Cookie
                filterContext.HttpContext.Response.Cookies[FormsAuthentication.FormsCookieName].Value = UserLoginTicket;
            }
        }

        //public abstract class ApplicationController : Controller
        //{
        //    protected ActionResult RespondTo(Action<FormatCollection> format)
        //    {
        //        return new FormatResult(format);
        //    }

        //    protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
        //    {
        //        return new ServiceStackJsonResult
        //        {
        //            Data = data,
        //            ContentType = contentType,
        //            ContentEncoding = contentEncoding
        //        };
        //    }
        //}

        //public class ServiceStackJsonResult : JsonResult
        //{
        //    public override void ExecuteResult(ControllerContext context)
        //    {
        //        HttpResponseBase response = context.HttpContext.Response;
        //        response.ContentType = !String.IsNullOrEmpty(ContentType) ? ContentType : "application/json";

        //        if (ContentEncoding != null)
        //        {
        //            response.ContentEncoding = ContentEncoding;
        //        }

        //        if (Data != null)
        //        {
        //            response.Write(JsonSerializer.SerializeToString(Data));
        //        }
        //    }
        //}    
    }
}
